﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CFZS.IDAL;
using System.Data.SqlClient;
using System.Data;
using CFZS.FrameWork.Utility;

namespace CFZS.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:baseUserPower
    /// </summary>
    public partial class baseUserPower : IbaseUserPower
    {
        public baseUserPower()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from baseUserPower");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(CFZS.Model.baseUserPower model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into baseUserPower(");
            strSql.Append("baseUser,basePower,isactive,lineno,createdat,createdby,updatedat,updateby,ip)");
            strSql.Append(" values (");
            strSql.Append("@baseUser,@basePower,@isactive,@lineno,@createdat,@createdby,@updatedat,@updateby,@ip)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@baseUser", SqlDbType.BigInt,8),
					new SqlParameter("@basePower", SqlDbType.BigInt,8),
					new SqlParameter("@isactive", SqlDbType.Int,4),
					new SqlParameter("@lineno", SqlDbType.Int,4),
					new SqlParameter("@createdat", SqlDbType.DateTime),
					new SqlParameter("@createdby", SqlDbType.VarChar,50),
					new SqlParameter("@updatedat", SqlDbType.DateTime),
					new SqlParameter("@updateby", SqlDbType.VarChar,50),
					new SqlParameter("@ip", SqlDbType.VarChar,50)};
            parameters[0].Value = model.baseUser;
            parameters[1].Value = model.basePower;
            parameters[2].Value = model.isactive;
            parameters[3].Value = model.lineno;
            parameters[4].Value = model.createdat;
            parameters[5].Value = model.createdby;
            parameters[6].Value = model.updatedat;
            parameters[7].Value = model.updateby;
            parameters[8].Value = model.ip;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CFZS.Model.baseUserPower model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update baseUserPower set ");
            strSql.Append("baseUser=@baseUser,");
            strSql.Append("basePower=@basePower,");
            strSql.Append("isactive=@isactive,");
            strSql.Append("lineno=@lineno,");
            strSql.Append("createdat=@createdat,");
            strSql.Append("createdby=@createdby,");
            strSql.Append("updatedat=@updatedat,");
            strSql.Append("updateby=@updateby,");
            strSql.Append("ip=@ip");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@baseUser", SqlDbType.BigInt,8),
					new SqlParameter("@basePower", SqlDbType.BigInt,8),
					new SqlParameter("@isactive", SqlDbType.Int,4),
					new SqlParameter("@lineno", SqlDbType.Int,4),
					new SqlParameter("@createdat", SqlDbType.DateTime),
					new SqlParameter("@createdby", SqlDbType.VarChar,50),
					new SqlParameter("@updatedat", SqlDbType.DateTime),
					new SqlParameter("@updateby", SqlDbType.VarChar,50),
					new SqlParameter("@ip", SqlDbType.VarChar,50),
					new SqlParameter("@id", SqlDbType.BigInt,8)};
            parameters[0].Value = model.baseUser;
            parameters[1].Value = model.basePower;
            parameters[2].Value = model.isactive;
            parameters[3].Value = model.lineno;
            parameters[4].Value = model.createdat;
            parameters[5].Value = model.createdby;
            parameters[6].Value = model.updatedat;
            parameters[7].Value = model.updateby;
            parameters[8].Value = model.ip;
            parameters[9].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from baseUserPower ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from baseUserPower ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CFZS.Model.baseUserPower GetModel(long id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,baseUser,basePower,isactive,lineno,createdat,createdby,updatedat,updateby,ip from baseUserPower ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
            parameters[0].Value = id;

            CFZS.Model.baseUserPower model = new CFZS.Model.baseUserPower();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = long.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["baseUser"] != null && ds.Tables[0].Rows[0]["baseUser"].ToString() != "")
                {
                    model.baseUser = long.Parse(ds.Tables[0].Rows[0]["baseUser"].ToString());
                }
                if (ds.Tables[0].Rows[0]["basePower"] != null && ds.Tables[0].Rows[0]["basePower"].ToString() != "")
                {
                    model.basePower = long.Parse(ds.Tables[0].Rows[0]["basePower"].ToString());
                }
                if (ds.Tables[0].Rows[0]["isactive"] != null && ds.Tables[0].Rows[0]["isactive"].ToString() != "")
                {
                    model.isactive = int.Parse(ds.Tables[0].Rows[0]["isactive"].ToString());
                }
                if (ds.Tables[0].Rows[0]["lineno"] != null && ds.Tables[0].Rows[0]["lineno"].ToString() != "")
                {
                    model.lineno = int.Parse(ds.Tables[0].Rows[0]["lineno"].ToString());
                }
                if (ds.Tables[0].Rows[0]["createdat"] != null && ds.Tables[0].Rows[0]["createdat"].ToString() != "")
                {
                    model.createdat = DateTime.Parse(ds.Tables[0].Rows[0]["createdat"].ToString());
                }
                if (ds.Tables[0].Rows[0]["createdby"] != null && ds.Tables[0].Rows[0]["createdby"].ToString() != "")
                {
                    model.createdby = ds.Tables[0].Rows[0]["createdby"].ToString();
                }
                if (ds.Tables[0].Rows[0]["updatedat"] != null && ds.Tables[0].Rows[0]["updatedat"].ToString() != "")
                {
                    model.updatedat = DateTime.Parse(ds.Tables[0].Rows[0]["updatedat"].ToString());
                }
                if (ds.Tables[0].Rows[0]["updateby"] != null && ds.Tables[0].Rows[0]["updateby"].ToString() != "")
                {
                    model.updateby = ds.Tables[0].Rows[0]["updateby"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ip"] != null && ds.Tables[0].Rows[0]["ip"].ToString() != "")
                {
                    model.ip = ds.Tables[0].Rows[0]["ip"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,baseUser,basePower,isactive,lineno,createdat,createdby,updatedat,updateby,ip ");
            strSql.Append(" FROM baseUserPower ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,baseUser,basePower,isactive,lineno,createdat,createdby,updatedat,updateby,ip ");
            strSql.Append(" FROM baseUserPower ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM baseUserPower ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from baseUserPower T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "baseUserPower";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}
